package com.shijun.bean;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhushijun on 2017/12/30.
 */

public class ConfigModle extends BaseModle {
    private List<GroupModle> groups;
    private int interval;//group子布局上下间隔
    private int backgroundId;

    public ConfigModle(){}

    public ConfigModle(int interval, int backgroundId, int width, int height, int marginLeft, int marginTop, int marginRight, int marginBottom) {
        this.interval = interval;
        this.backgroundId = backgroundId;
        setWidthAndHeight(width, height);
        setMargin(marginLeft, marginTop, marginRight, marginBottom);
        groups = new ArrayList<>();
    }

    //单个添加GroupModle
    public void setGroupModle(GroupModle group) {
        groups.add(group);
    }

    //批量添加GroupModle
    public void setGroupModles(GroupModle group, int count) {
        for (int i = 0; i < count; i++) {
            if (i != 0) {
                groups.add(group.clone());
            } else {
                groups.add(group);
            }
        }
    }

    //获取Image的个数
    public int getCount() {
        int count = 0;
        for (int i = 0; i < groups.size(); i++) {
            for (int j = 0; j < groups.get(i).getImages().size(); j++) {
                count++;
            }
        }
        return count;
    }

    public int getWidthByWeight(int _Y,int rootViewWidth){
        int residue = rootViewWidth;
        GroupModle group = getGroup(_Y);
        List<ImageModle> images = group.getImages();
        if(images!=null&&images.size()!=0) {
            residue = residue - marginLeft - marginRight - (group.getImages().size() - 1) * interval;
        }else{
           return 0;
        }

        int count = 0;
        for(ImageModle img:images){
            count = count + img.getWeight();
        }
        return residue/count;
    }

    //通过位于第几项获取该子项的左上角的位置和宽高
    public int[] getPositionByItem(int item) {
        int itemX = 0;
        int itemY = 0;
        int count = 0;
        for (int i = 0; i < groups.size(); i++) {
            for (int j = 0; j < groups.get(i).getImages().size(); j++) {
                if (count == item) {
                    itemX = i;
                    itemY = j;
                }
                count++;
            }
        }

        int _X = marginLeft;
        int _Y = marginTop;
        for (int i = 0; i <= itemX; i++) {
            GroupModle group = groups.get(i);
            if (i == itemX) {
                _Y = _Y + group.getMarginTop();
                for (int j = 0; j <= itemY; j++) {
                    ImageModle imageModle = group.getImage(j);
                    if (j == itemY) {
                        _X = _X + imageModle.getMarginLeft();
                    } else {
                        _X = _X + imageModle.getMarginLeft() + imageModle.getMarginRight() + imageModle.getWidth();
                    }
                }
            } else {
                _Y = _Y + group.getHeight() + group.getMarginTop() + group.getMarginBottom();
            }
        }
        return new int[]{_X, _Y, width, height};
    }

    public List<GroupModle> getGroups() {
        return groups;
    }

    public GroupModle getGroup(int position) {
        return groups.get(position);
    }

    public int getInterval() {
        return interval;
    }

    public void setInterval(int interval) {
        this.interval = interval;
    }

    public int getBackgroundId() {
        return backgroundId;
    }

    public void setBackgroundId(int backgroundId) {
        this.backgroundId = backgroundId;
    }

    //批量添加图片url
    public void setImagesUrl(List<String> imagsUrl){
        if(groups==null||groups.size()==0||imagsUrl==null||imagsUrl.size()==0){
            return;
        }
        int position = 0;
        for (int i = 0; i < groups.size(); i++) {
            List<ImageModle> imgs = groups.get(i).getImages();
            for (int j = 0; j < imgs.size(); j++) {
                if (position < imagsUrl.size()) {
                    imgs.get(j).setImageUrl(imagsUrl.get(position));
                }
                position++;
            }
        }
    }

    @Override
    public String toString() {
        return "ConfigModle{" +
                "width=" + width +
                ", height=" + height +
                ", marginTop=" + marginTop +
                ", marginBottom=" + marginBottom +
                ", groups=" + groups +
                ", interval=" + interval +
                ", marginLeft=" + marginLeft +
                ", marginRight=" + marginRight +
                ", backgroundId=" + backgroundId +
                '}';
    }
}
